package com.bjtg.threegoclinet.util;

import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import com.bjtg.threegoclinet.base.MyApplication;

/**
 * Created by 傅令杰 on 2017/4/22
 */

public final class LattePreference {

    /**
     * 提示:
     * Activity.getPreferences(int mode)生成 Activity名.xml 用于Activity内部存储
     * PreferenceManager.getDefaultSharedPreferences(Context)生成 包名_preferences.xml
     * Context.getSharedPreferences(String name,int mode)生成name.xml
     */
    private static final SharedPreferences PREFERENCES =
            PreferenceManager.getDefaultSharedPreferences(MyApplication.getApplicationMyContext());
    private static final String APP_PREFERENCES_KEY = "profile";
    public static final String USER_ID = "userId";
    public static final String OPERATION_ID = "operation_id";
    public static final String IS_FIRSTEXECUTE = "isFirstExecute";
    public static final String IS_OPENCFM = "isOpenCFM";
    public static final String IS_ONLINE = "isOnline";
    public static final String IS_UPDATE = "isUpdate";
    public static final String PATCH_URL = "patchUrl";

    private static SharedPreferences getAppPreference() {
        return PREFERENCES;
    }

    public static void setAppProfile(String val) {
        getAppPreference()
                .edit()
                .putString(APP_PREFERENCES_KEY, val)
                .apply();
    }

    public static String getAppProfile() {
        return getAppPreference().getString(APP_PREFERENCES_KEY, null);
    }

    /*public static JSONObject getAppProfileJson() {
        final String profile = getAppProfile();
        return JSON.parseObject(profile);
    }*/

    public static void removeAppProfile() {
        getAppPreference()
                .edit()
                .remove(APP_PREFERENCES_KEY)
                .apply();
    }

    public static void clearAppPreferences() {
        getAppPreference()
                .edit()
                .clear()
                .apply();
    }

    public static void addPatchUrl(String key, String url){
        getAppPreference()
                .edit()
                .putString(key,url)
                .apply();
    }

    public static String getPatchUrl(String key){
        return getAppPreference().getString(key,"");
    }
    public static void setAppFlag(String key, boolean flag) {
        getAppPreference()
                .edit()
                .putBoolean(key, flag)
                .apply();
    }

    public static boolean getAppFlag(String key) {
        return getAppPreference()
                .getBoolean(key, false);
    }

    public static void addCustomAppProfile(String key, String val) {
        getAppPreference()
                .edit()
                .putString(key, val)
                .apply();
    }

    public static void addIsUpdate(String key, boolean val) {
        getAppPreference()
                .edit()
                .putBoolean(key, val)
                .apply();
    }

    public static boolean getIsUpdate(String key) {
        return getAppPreference().getBoolean(key, false);
    }

    public static String getCustomAppProfile(String key) {
        return getAppPreference().getString(key, "");
    }

    public static void addUserId(String key, String val) {
        getAppPreference()
                .edit()
                .putString(key, val)
                .apply();
    }

    public static String getUserId(String key) {
        return getAppPreference().getString(key, "1");
    }

    public static void addOperationId(String key, String val) {
        getAppPreference()
                .edit()
                .putString(key, val)
                .apply();
    }

    public static String getOperationId(String key) {
        return getAppPreference().getString(key, "1");
    }

    public static void addIsFirstExecute(String key, boolean isFirst) {
        getAppPreference().edit()
                .putBoolean(key, isFirst)
                .apply();
    }

    public static boolean getIsFirstExecute(String key) {
        return getAppPreference().getBoolean(key, true);
    }

    public static void addIsOpenCFM(String key, boolean isOpen) {
        getAppPreference().edit()
                .putBoolean(key, isOpen)
                .apply();
    }

    public static boolean getIsOpenCFM(String key) {
        return getAppPreference().getBoolean(key, true);
    }

    public static void addIsOnline(String key, boolean isOnline) {
        getAppPreference().edit()
                .putBoolean(key, isOnline)
                .apply();
    }

    public static boolean getIsOnline(String key) {
        return getAppPreference().getBoolean(key, true);
    }

}
